/*
 * File: BALC.c
 *
 * Code generated for Simulink model 'BALC'.
 *
 * Model version                  : 3.109
 * Simulink Coder version         : 9.4 (R2020b) 29-Jul-2020
 * C/C++ source code generated on : Thu Sep  8 13:42:19 2022
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: NXP->Cortex-M4
 * Code generation objective: MISRA C:2012 guidelines
 * Validation result: Not run
 */

#include "BALC.h"
#include "BALC_private.h"
#include "RTE.h"

/* Named constants for Chart: '<S2>/BALC' */
#define IN_BalContinue                 ((uint8_T)1U)
#define IN_BalCtrl                     ((uint8_T)2U)
#define IN_BalPause                    ((uint8_T)3U)
#define IN_BalVoltCompare              ((uint8_T)4U)
#define IN_Init                        ((uint8_T)5U)

/* Block signals (default storage) */
B_BALC_T BALC_B;

/* Block states (default storage) */
DW_BALC_T BALC_DW;

/* Real-time model */
static RT_MODEL_BALC_T BALC_M_;
RT_MODEL_BALC_T *const BALC_M = &BALC_M_;

/* Exported data definition */

/* Definition for custom storage class: ExportToFile */
uint8_T balc_enum_balPauseSts = ((uint8_T)0U);/* '<S2>/BALC' */

/* ~{>y:bT]M#T-Rr~} */
uint32_T balc_enum_balTime = 0U;       /* '<S2>/BALC' */

/* ~{>y:bJ1<d~} */
boolean_T balc_flg_balanceEn = false;  /* '<S2>/BALC' */

/* ~{>y:b?*FtJ9D\~} */
uint16_T balc_mv_balOpenCVlt = ((uint16_T)0U);/* '<S2>/BALC' */

/* ~{>y:b?*FtJ15D5gQ9~}(0:invalid) */

/* Output and update for atomic system: '<S2>/BALC' */
void BALC_o(void)
{
    boolean_T guard1 = false;
    if (BALC_DW.is_active_c1_BALC == 0U)
    {
        BALC_DW.is_active_c1_BALC = 1U;
        BALC_DW.is_c1_BALC = IN_Init;
        balc_mv_balOpenCVlt = 0U;
        balc_flg_balanceEn = false;
        BALC_DW.count = 0U;
    }
    else
    {
        guard1 = false;
        switch (BALC_DW.is_c1_BALC)
        {
          case IN_BalContinue:
            if ((Rte_enum_cellVoltSmp_GetVal() == 0) && (soce_pct_SOC >=
                    Pccfg_balMinSOC) && (Rte_mv_CellMinVol_GetVal() >=
                    Pccfg_balMinCVlt) && (fltm_enum_dsgFltLv <= 2) &&
                    (fltm_enum_chgFltLv <= 2) && (rte_flg_balanceTempFlt_GetVal()
                 == 0) && (rte_enum_balCellNum_GetVal() >= 1))
            {
                BALC_DW.is_c1_BALC = IN_BalCtrl;
                balc_flg_balanceEn = true;
                balc_enum_balPauseSts = 0U;

                /*
                 * Block description for '<S15>/Function Caller':
                 *  Lib_Balance.h
                 */
                Balc_BalanceRequestCtrl();
            }
            break;

          case IN_BalCtrl:
            if (rte_flg_clrBalTime_GetVal() != 0)
            {
                /*
                 * Block description for '<S14>/Function Caller':
                 *  Lib_Balance.h
                 */
                Balc_BalanceCtrlSet();
                BALC_DW.is_c1_BALC = IN_BalVoltCompare;

                /*
                 * Block description for '<S17>/Function Caller':
                 *  Lib_Balance.h
                 */
                Balc_BalanceVoltageCompare();
                balc_mv_balOpenCVlt = (uint16_T)(Rte_mv_CellMinVol_GetVal() +
                    Pccfg_balVDiff);
                balc_enum_balTime = 0U;
                BALC_DW.count = 0U;
            }
            else if (balc_enum_balTime >= 288000U)
            {
                balc_enum_balPauseSts = 4U;
                guard1 = true;
            }
            else if (BALC_B.DelayTimesforS4.Compare_ih)
            {
                balc_enum_balPauseSts = 1U;
                guard1 = true;
            }
            else if (soce_pct_SOC < Pccfg_balMinSOC)
            {
                balc_enum_balPauseSts = 2U;
                guard1 = true;
            }
            else if (Rte_mv_CellMinVol_GetVal() < Pccfg_balMinCVlt)
            {
                balc_enum_balPauseSts = 3U;
                guard1 = true;
            }
            else
            {
                /* . */
                if ((Rte_enum_cellVoltSmp_GetVal() != 0) ||
                        (rte_flg_balanceTempFlt_GetVal() != 0) ||
                        (fltm_enum_dsgFltLv >= 3) || (fltm_enum_chgFltLv >= 3) ||
                    (Rte_mv_SplyPwrVolt_GetVal() < Pccfg_balSplyVldVlt) ||
                        (rte_flg_sampleFinish_GetVal() == 0))
                {
                    balc_enum_balPauseSts = 10U;
                    guard1 = true;
                }
                else
                {
                    balc_enum_balTime = balc_enum_balTime + 1U;
                }
            }
            break;

          case IN_BalPause:
            if (rte_flg_clrBalTime_GetVal() != 0)
            {
                /*
                 * Block description for '<S14>/Function Caller':
                 *  Lib_Balance.h
                 */
                Balc_BalanceCtrlSet();
                BALC_DW.is_c1_BALC = IN_BalVoltCompare;

                /*
                 * Block description for '<S17>/Function Caller':
                 *  Lib_Balance.h
                 */
                Balc_BalanceVoltageCompare();
                balc_mv_balOpenCVlt = (uint16_T)(Rte_mv_CellMinVol_GetVal() +
                    Pccfg_balVDiff);
                balc_enum_balTime = 0U;
                BALC_DW.count = 0U;
            }
            else
            {
                /*   */
                if (((balc_enum_balPauseSts == 1) &&
                        BALC_B.DelayTimesforS1.Compare_ih) ||
                        ((balc_enum_balPauseSts == 2) && (soce_pct_SOC >=
                        Pccfg_balMinSOC + 500)) || ((balc_enum_balPauseSts == 3)
                     && (Rte_mv_CellMinVol_GetVal() >= Pccfg_balMinCVlt + 150)))
                {
                    BALC_DW.is_c1_BALC = IN_BalCtrl;
                    balc_flg_balanceEn = true;
                    balc_enum_balPauseSts = 0U;

                    /*
                     * Block description for '<S15>/Function Caller':
                     *  Lib_Balance.h
                     */
                    Balc_BalanceRequestCtrl();
                }
            }
            break;

          case IN_BalVoltCompare:
            if ((Rte_enum_cellVoltSmp_GetVal() == 0) && (soce_pct_SOC >=
                    Pccfg_balMinSOC) && (Rte_mv_CellMinVol_GetVal() >=
                    Pccfg_balMinCVlt) && (fltm_enum_dsgFltLv <= 2) &&
                    (fltm_enum_chgFltLv <= 2) && (rte_flg_balanceTempFlt_GetVal()
                 == 0) && (rte_enum_balCellNum_GetVal() >= 1))
            {
                BALC_DW.is_c1_BALC = IN_BalCtrl;
                balc_flg_balanceEn = true;
                balc_enum_balPauseSts = 0U;

                /*
                 * Block description for '<S15>/Function Caller':
                 *  Lib_Balance.h
                 */
                Balc_BalanceRequestCtrl();
            }
            break;

          default:
            /* case IN_Init: */
            if (((BALC_B.Subtract10 <= 5) || ((BALC_B.Subtract10 <= 15) &&
                    (soce_pct_SOC <= 9000))) && (soce_pct_SOC >= 5000) &&
                    (Rte_mv_CellMinVol_GetVal() >= 3250) &&
                    (Rte_enum_cellVoltSmp_GetVal() == 0) &&
                    (Rte_enum_cellTempSmp_GetVal() == 0) &&
                    (Rte_mv_SplyPwrVolt_GetVal() >= Pccfg_balSplyVldVlt) &&
                    (rte_flg_sampleFinish_GetVal() == 1) &&
                    (((rte_ms_sysRunTime_GetVal() > 2500U) &&
                      (rte_ms_sysRunTime_GetVal() < 5500U) &&
                      (Rte_enum_BalAlwSts_GetVal() == 1)) ||
                     ((BALC_B.DataTypeConversion1 <= 2) && (BALC_DW.count > 18000))))
            {
                BALC_DW.is_c1_BALC = IN_BalVoltCompare;

                /*
                 * Block description for '<S17>/Function Caller':
                 *  Lib_Balance.h
                 */
                Balc_BalanceVoltageCompare();
                balc_mv_balOpenCVlt = (uint16_T)(Rte_mv_CellMinVol_GetVal() +
                    Pccfg_balVDiff);
                balc_enum_balTime = 0U;
                BALC_DW.count = 0U;
            }
            else if ((balc_enum_balTime < 288000U) && (balc_enum_balTime > 0U) &&
                     (Rte_enum_BalAlwSts_GetVal() == 0) &&
                     (rte_enum_balCellNum_GetVal() >= 1) &&
                     (rte_ms_sysRunTime_GetVal() >= 5500U))
            {
                BALC_DW.is_c1_BALC = IN_BalContinue;
            }
            else
            {
                if (BALC_B.DataTypeConversion1 <= 2)
                {
                    BALC_DW.count++;
                }
            }
            break;
        }

        if (guard1)
        {
            BALC_DW.is_c1_BALC = IN_BalPause;
            balc_flg_balanceEn = false;

            /*
             * Block description for '<S16>/Function Caller':
             *  Lib_Balance.h
             */
            Balc_BalanceRequestPause();
        }
    }
}

/* Output and update for function-call system: '<S1>/BALC' */
void BALC_a(void)
{
    int16_T Abs;
    Abs = (int16_T)((int16_T)(Rte_ma_BatCurr_GetVal() / 1000U) - 1000);
    if (Abs > 1000)
    {
        Abs = 1000;
    }
    else
    {
        if (Abs < -1000)
        {
            Abs = -1000;
        }
    }

    if (Abs < 0)
    {
        Abs = (int16_T)-Abs;
    }

    BALC_B.DataTypeConversion1 = (uint16_T)Abs;
    BALC_B.Subtract10 = (uint8_T)(Rte_c_CellMaxTmp_GetVal() -
        Rte_c_CellMinTmp_GetVal());
    DelayTimesSe((((vewm_enum_workingMode == 3) && (Rte_mv_CellMaxVol_GetVal() >=
                    3500)) || ((vewm_enum_workingMode == 4) &&
                   (Rte_mv_CellMaxVol_GetVal() >= 3510))) &&
                 (Rte_ma_BatCurr_GetVal() < 999000U), &BALC_B.DelayTimesforS4,
                 &BALC_DW.DelayTimesforS4, 10);
    DelayTimesSe((vewm_enum_FastChgSts == 4) || (vewm_enum_SlowChgSts == 4),
                 &BALC_B.DelayTimesforS1, &BALC_DW.DelayTimesforS1, 500);
    BALC_o();
}

/* Model step function */
void TASK_BALE(void)
{
    BALC_a();
}

/* Model initialize function */
void BALC_initialize(void)
{
    /* (no initialization code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
